import java.io.*;
import java.util.*;
/**
    TextFileReader is an Iterable class that can be used to iterate through
    a text file, one Character at a time.
    Use it as in the following example:
    public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    System.out.print("Enter a filename: ");
	    String fname=sc.nextLine();
	    TextFileReader f = new TextFileReader(fname);
	    for (Character ch:f) System.out.print(ch);
	}
	@author Bruce Martin
*/
public class TextFileReader implements Iterable<Character> {

/** textBuffer is an array of Character elements from the text file. */
private Character[] textBuffer;
/** size is the number of Characters in textBuffer */
int size;
/** A one-String-parameter constructor opens the file represented by the String,
	from the same directory as the program file, and reads the contents into
	textBuffer.
	@param fname is the name of the text file.
*/
public TextFileReader(String fname) {
	    textBuffer = new Character[100];
	    readFromFile(fname);
	}
/** ensureCapacity doubles the capacity of the Character array and copies the old
	content to it, replacing the textBuffer array, if the current array is less than
	the new capacity needed.
	@param newCapacity is the capacity requested
*/
private void ensureCapacity(int newCapacity) {
	if (textBuffer.length<=newCapacity) {
		Character[] newBuffer = new Character[textBuffer.length*2];
		for (int i=0; i<size; i++) newBuffer[i] = textBuffer[i];
		textBuffer = newBuffer;
	}
}
/** readFromFile opens the text file with the given name and reads all of it into
	textBuffer, including newlines.
	@param fname is the name of the text file in the current directory.
*/
public void readFromFile(String fname) {
	    try{BufferedReader iStream
	                 = new BufferedReader(
	                  new FileReader(fname)
	                   );
	        String stringIn;
	        Scanner sc = new Scanner(iStream);
	        while (sc.hasNext())
	            {stringIn=sc.nextLine();
				 ensureCapacity(size+stringIn.length()+1);
	             for (int i=0; i<stringIn.length(); i++)
	                 textBuffer[size++] = stringIn.charAt(i);
	             textBuffer[size++] = '\n';
	             }
	        iStream.close();
	    }
	    catch (FileNotFoundException e)
	       {System.out.println("Did not find "+fname);}
	    catch (IOException e) {}
    }
/** iterator returns an iterator for the input file. */
public Iterator<Character> iterator() {
	return new FileIterator();
}

/** The private inner class FileIterator implements an Iterator of Characters */
private class FileIterator implements Iterator<Character> {
	/** curr is the current position in the textBuffer array */
	private int curr;
	public FileIterator() {
	}
	/** next() returns the next Character and updates the current index
	    @return the next Character from the file
	*/
	public Character next() {
		return textBuffer[curr++];
	}
	/** hasNext() returns whether there is another Character
	    @return true if there is another Character in the buffer, else false.
	*/
	public boolean hasNext() {
		return curr<size;
	}
	/** remove() is not implemented
		@throws UnsupportedOperationException
	*/
	public void remove() {
		throw new UnsupportedOperationException("remove is not supported");
	}
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a filename: ");
    String fname=sc.nextLine();
    TextFileReader f = new TextFileReader(fname);
    for (Character ch:f) System.out.print(ch);
}
}